package com.devicemantra.coordinate.ds.tests;


import com.devicemantra.coordinate.jaxb.*;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.namespace.QName;

import com.devicemantra.coordinate.dataobjects.CDepartmentFactory;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;

@SuppressWarnings("serial")
public class JAXBMarshaller extends HttpServlet 
{
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
	throws IOException 
	{
		JAXBContext context = null;
		Marshaller marshaller = null;
		try {
			 context = 	JAXBContext.newInstance("com.devicemantra.coordinate.jaxb");
			 
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		
		try {
			marshaller =   context.createMarshaller();
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		ObjectFactory  factory = new ObjectFactory();
		Department lDepartment = factory.createDepartment();
		DepartmentInfo lCDepartmentInfo = factory.createDepartmentInfo();
		lCDepartmentInfo.setCDepartmentName("JUSTIC_1");
		lCDepartmentInfo.setCDescription("Near Muroor police station");
		lCDepartmentInfo.setCEmailAddress("amith@ctg.ae");
		lCDepartmentInfo.setCLogo("justice.jpg");
		lCDepartmentInfo.setCMobileNumber("+971559266628");
		lCDepartmentInfo.setCPersonDesignation("IT Manager");
		
		PersonName lPersonName = new PersonName();
		lPersonName.setFirstName("Amith");
		lPersonName.setLastName("Nambiar");
		
		NumContacts lNumContacts = factory.createNumContacts();
		lNumContacts.setEmailContacts(120);
		lNumContacts.setNumDeskContacts(12);
		lNumContacts.setNumFaxContacts(20);
		lNumContacts.setNumTeleContacts(20);
		lNumContacts.setNumMobileContacts(13);
		lCDepartmentInfo.setCPersonName(lPersonName);
		lCDepartmentInfo.setCNumContacts(lNumContacts);
		lDepartment.setCDepartmentInfo(lCDepartmentInfo);
		
		DepartmentList lDeptList = factory.createDepartmentList();
		lDeptList.getCDepartment().add(lDepartment);
		lDeptList.getCDepartment().add(lDepartment);
		lDeptList.getCDepartment().add(lDepartment);
		
		Status lStatus = factory.createStatus();
		lStatus.setCReason("Could not find a location with that id !");
		lStatus.setCRespCode(404);
		
		/* Create a Text message Structure */
		TextMessage lTextMessage = factory.createTextMessage();
		lTextMessage.setCTextPayload("Welcome to www.devicemantra.com CoOrdinate");
		
		MessageDetails lMesgDetails = factory.createMessageDetails();
		lMesgDetails.setCMessagePriority(2);
		
		
		Date lDate = factory.createDate();
		lDate.setCDay(23);
		lDate.setCMonth(11);
		lDate.setCYear(2010);
		
		Time lTime = factory.createTime();
		lTime.setCHour(14);
		lTime.setCMinute(55);
		
		MessageChannels lChannels = factory.createMessageChannels();
		lChannels.getCMessageChannel().add(MessageChannel.CFAX);
		lChannels.getCMessageChannel().add(MessageChannel.CMOBILE);
		lChannels.getCMessageChannel().add(MessageChannel.CDESK);
		
		LocationIds lLocations = factory.createLocationIds();
		lLocations.getCLocationId().add("LAW_2");
		lLocations.getCLocationId().add("POLOCE_2");
		lLocations.getCLocationId().add("HEALTH_2");
		
		TargetLocations lTargetLocs = factory.createTargetLocations();
		lTargetLocs.setCLocationIds(lLocations);
		
		GroupNameList lGList = factory.createGroupNameList();
		lGList.getCGroupName().add("POLICE");
		lTargetLocs.setCGroupNameList(lGList);
		
		DeptWithinState lDeptState = factory.createDeptWithinState();
		lDeptState.setCStateName("DUBAI");
		lDeptState.getCDepartmentName().add("POLICE");
		lDeptState.getCDepartmentName().add("LAW");
		lDeptState.getCDepartmentName().add("EDUCATION");
		
		DeptWithinState lDeptState1 = factory.createDeptWithinState();
		lDeptState1.setCStateName("SHARJAH");
		lDeptState1.getCDepartmentName().add("ENVIRONMENT");
		lDeptState1.getCDepartmentName().add("LAW");
		
		
		
		
		Request lRequest = factory.createRequest();
		lRequest.setCOperationType(OperationType.SEND_MESSAGE);
		
		DeptWithinStateList lDSList = factory.createDeptWithinStateList();
		lDSList.getCDeptWithinState().add(lDeptState);
		lDSList.getCDeptWithinState().add(lDeptState1);
		lTargetLocs.setCDeptWithinStateList(lDSList);
		
		
		
		State lState = factory.createState();
		lState.setName("Abu Dhabi");
		lState.setCapital("Al Ain");
		
		Message lMessage = factory.createMessage();
		lMessage.setCTextMessage(lTextMessage);
		lMessage.setCMessageType(MessageType.CTEXT_MESSAGE);
		
		MessageDetails lMsgDetails = factory.createMessageDetails();
		lMessage.setCMessageDetails(lMsgDetails);
		
		lMessage.getCMessageDetails().setCDate(lDate);
		lMessage.getCMessageDetails().setCTime(lTime);
		lMessage.getCMessageDetails().setCMessageChannels(lChannels);
		lMessage.getCMessageDetails().setCTargetLocations(lTargetLocs);
		
		
		
		RequestData lData = factory.createRequestData();
		//lData.setCState(lState);
		lData.setCMessage(lMessage);
		lRequest.setCRequestData(lData);
		
		AudioMessage lAudioMesg = factory.createAudioMessage();
		
		
		
		JAXBElement<Request> lCRequest = factory.createCRequest(lRequest);
		JAXBElement<TextMessage> lCTextMessage = factory.createCTextMessage(lTextMessage);
		JAXBElement<DepartmentList> lCDepartmentList = factory.createCDepartmentList(lDeptList);
		JAXBElement<Status> lCStatus = factory.createCStatus(lStatus);
				
		resp.setContentType("text/xml");
		PrintWriter lPwriter = resp.getWriter();
		try {
			//marshaller.setProperty("com.sun.xml.internal.bind.namespacePrefixMapper", new PrefixMapper());
			//marshaller.setProperty("jaxb.noNamespaceSchemaLocation ", "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"   xsi:schemaLocation=\"http://www.example.com/Report  http://www.example.com/Report.xsd");
			//marshaller.setProperty("jaxb.noNamespaceSchemaLocation ", "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"   xsi:schemaLocation=\"http://www.example.com/Report");
			
			//marshaller.marshal(lCDepartmentList, lPwriter);
			//marshaller.marshal(lCDepartmentList, System.out);
			//marshaller.marshal(lCStatus, lPwriter);
			//marshaller.marshal(lCStatus, System.out);
			marshaller.marshal(lCRequest, lPwriter);
			marshaller.marshal(lCRequest, System.out);
			
			
			//marshaller.marshal(lDepartment, lPwriter);
			//marshaller.marshal( new JAXBElement(
					//  new QName("http://www.devicemantra.com/coordinate","CDepartment"), CDepartment.class, lDepartment ), lPwriter);

		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		
		
	}
	
}
